import React, {Component} from "react";
import ReactEcharts from "echarts-for-react";
import echarts from "echarts/lib/echarts";
import picture from '../timg.jpg';
import axios from "axios";
import {server_addr} from "../index";
import {Table} from "antd";

const columns = [
    {
        title: '排名',
        dataIndex: 'rank',
        key: 'rank',
        render: text => {
            if(text == "1"){
                return <span style={{
                    borderRadius: "1vw",
                    background: "red",
                    display: "block",
                    width: "1.5vw",
                    height: "1.5vw",
                    lineHeight: "1.5vw",
                    margin: "0 1vw",
                    textAlign: "center",
                    color: "white"}}>{text}</span>
            }else if(text == "2"){
                return <span style={{
                    borderRadius: "1vw",
                    background: "darkorange",
                    display: "block",
                    width: "1.5vw",
                    height: "1.5vw",
                    lineHeight: "1.5vw",
                    margin: "0 1vw",
                    textAlign: "center",
                    color: "white"}}>{text}</span>
            }else if(text == "3"){
                return <span style={{
                    borderRadius: "1vw",
                    background: "orange",
                    display: "block",
                    width: "1.5vw",
                    height: "1.5vw",
                    lineHeight: "1.5vw",
                    margin: "0 1vw",
                    textAlign: "center",
                    color: "white"}}>{text}</span>
            }else{
                return <span style={{
                    borderRadius: "1vw",
                    background: "#3398DB",
                    display: "block",
                    width: "1.5vw",
                    height: "1.5vw",
                    lineHeight: "1.5vw",
                    margin: "0 1vw",
                    textAlign: "center",
                    color: "white"}}>{text}</span>
            }
        }
    },
    {
        title: '股票名称',
        dataIndex: 'name',
        key: 'name',
    },
    {
        title: '资金(万元)',
        dataIndex: 'money',
        key: 'money',
    }
];
const pagination = {
    pageSize: 5
};

export default class Fund_2 extends Component {
    constructor(props) {
        super(props);
        this.state = {
            option_1: {},
            option_2: {},
            top10: [],
            bottom10: [],
            default: "83%"
        }
        this.getData = this.getData.bind(this);
    }


    getData(){
        var stock = window.location.href.split('?')[1].split('=')[1];
        // 行业前十资金
        axios.get(server_addr+"/capital/top10?stock="+stock,{}).then((res)=>{
            var data = [];
            var i = 1;
            for(var key in res.data){
                data.push({
                    rank: i,
                    name: key,
                    money: res.data[key]
                });
                i++;
            }
            this.setState({
                top10: data
            })
            console.log(data)
        }).catch(function (error) {
            alert('未连接到服务器！');
        })
        // 行业后十资金
        axios.get(server_addr+"/capital/bottom10?stock="+stock,{}).then((res)=>{
            var data = [];
            var i = 1;
            for(var key in res.data){
                data.push({
                    rank: i,
                    name: key,
                    money: res.data[key]
                });
                i++;
            }
            this.setState({
                bottom10: data
            })
            console.log(data)
        }).catch(function (error) {
            alert('未连接到服务器！');
        })
        //主力资金净流入占比
        // axios.get(server_addr+"/capital/mainFlowRatio?stock="+stock,{}).then((res)=>{
        //     this.setState({
        //         option_1: {
        //             series: [{
        //                 type: 'pie',
        //                 radius: ['55%', '85%'],
        //                 data: [
        //                     {
        //                         name:'主力资金净流入占比',
        //                         value:res.data,
        //                         itemStyle:{
        //                             color: {
        //                                 type: 'linear',
        //                                 colorStops: [{
        //                                     offset: 0, color: '#a71d2a' // 0% 处的颜色
        //                                 }, {
        //                                     offset: 1, color: 'pink' // 100% 处的颜色
        //                                 }],
        //                             },
        //                         },
        //                         label:{
        //                             show:true,  //单独显示该数据项
        //                             formatter:'{d}%'
        //                         }},
        //                     {
        //                         name:'暂无',
        //                         value:12.4,
        //                         itemStyle:{
        //                             color: '#ccc',
        //                         }
        //                     }],
        //                 label:{
        //                     normal:{//默认不显示数据
        //                         show : false,
        //                         position:'center',
        //                     }
        //                 }
        //             }]
        //         }
        //     })
        // }).catch(function (error) {
        //     alert('未连接到服务器！');
        // })

        //资金流向 依次是小单、中单、大单、超大单、涨跌幅
        axios.get(server_addr+"/capital/flow?stock="+stock,{}).then((res)=>{
            let dates = [];
            let data_1 = [];
            let data_2 = [];
            let data_3 = [];
            let data_4 = [];
            let data_5 = [];
            for(var key in res.data){
                dates.push(key);
                data_1.push(res.data[key].split(',')[0]);
                data_2.push(res.data[key].split(',')[1]);
                data_3.push(res.data[key].split(',')[2]);
                data_4.push(res.data[key].split(',')[3]);
                data_5.push(res.data[key].split(',')[4]);
            }
            this.setState({
                option_2: {
                    legend: [{
                        top: 5,
                        data: ['行业平均涨跌幅','N日行业平均超大单资金净流入','N日行业平均大单资金净流入'],
                        itemHeight: 5,
                        itemWidth: 10,
                        textStyle: {
                            fontSize: 16
                        }
                    },{
                        top: 40,
                        data: ['N日行业平均中单资金净流入','N日行业平均小单资金净流入'],
                        itemHeight: 5,
                        itemWidth: 10,
                        textStyle: {
                            fontSize: 16
                        }
                    }],
                    tooltip: {
                        trigger: 'axis',
                        transitionDuration:0,
                        axisPointer: {
                            type: 'line'
                        }
                    },
                    grid: [
                        {
                            left: '10%',
                            right: '10%',
                            bottom: 120,
                            top: 80
                        }
                    ],
                    dataZoom: [{
                        type: 'slider',
                        realtime: false,
                        start: 50,
                        end: 100,
                        top: 500,
                        height: 20,
                        handleIcon: 'M10.7,11.9H9.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4h1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z',
                        handleSize: '120%'
                    }, {
                        type: 'inside',
                        start: 40,
                        end: 70,
                        top: 30,
                        height: 20
                    }],
                    xAxis: [{
                        type: 'category',
                        data: dates.reverse(),
                        boundaryGap: true,
                        axisLine: {lineStyle: {color: '#777'}},
                        axisLabel: {
                            formatter: function (value) {
                                return echarts.format.formatTime('MM-dd', value);
                            },
                            fontSize: 10,
                            rotate: 35,
                        },
                        min: 'dataMin',
                        max: 'dataMax',
                        axisPointer: {
                            show: true
                        }
                    }],
                    yAxis: [{
                        scale: true,
                        name: '万元        ',
                        axisLine: {lineStyle: {color: '#777'}},
                        splitLine: {show: true},
                        axisTick: {show: false},
                        axisLabel: {
                            fontSize: 10
                        }
                    },{
                        scale: true,
                        name: '        %',
                        axisLine: {lineStyle: {color: '#777'}},
                        axisTick: {show: false},
                        axisLabel: {
                            fontSize: 10
                        }
                    }],
                    series: [
                        {
                            name: '行业平均涨跌幅',
                            type: 'line',
                            yAxisIndex: 1,
                            smooth: 0.5,
                            itemStyle:{
                                normal:{
                                    lineStyle: {
                                        width: 3.5
                                    },
                                    color:'#ef232a',
                                }
                            },
                            // itemStyle:{
                            //     normal:{
                            //         color: function(params) {
                            //             if (params.value > 0) {
                            //                 return "#ef232a";
                            //             } else{
                            //                 return "#14b143";
                            //             }
                            //         }
                            //     }
                            // },
                            data: data_5.reverse()
                        },
                        // {
                        //     name: 'N日主力资金净流入',
                        //     type: 'line',
                        //     itemStyle:{
                        //         normal:{
                        //             lineStyle: {
                        //                 width: 3.5
                        //             },
                        //             color: '#E87C25'
                        //         }
                        //     },
                        //     data: data_1,
                        // },
                        {
                            name: 'N日行业平均超大单资金净流入',
                            type: 'line',
                            symbol: 'none',
                            smooth: 0.5,
                            itemStyle:{
                                normal:{
                                    lineStyle: {
                                        width: 3.5
                                    },
                                    color: '#14b143'
                                }
                            },
                            data: data_4.reverse()
                        },
                        {
                            name: 'N日行业平均大单资金净流入',
                            type: 'line',
                            symbol: 'none',
                            smooth: 0.5,
                            itemStyle:{
                                normal:{
                                    lineStyle: {
                                        width: 3.5
                                    },
                                    color: '#ba55d3'
                                }
                            },
                            data: data_3.reverse(),
                        },
                        {
                            name: 'N日行业平均中单资金净流入',
                            type: 'line',
                            symbol: 'none',
                            smooth: 0.5,
                            itemStyle:{
                                normal:{
                                    lineStyle: {
                                        width: 3.5
                                    },
                                    color: '#60C0DD'
                                }
                            },
                            data: data_2.reverse(),
                        },
                        {
                            name: 'N日行业平均小单资金净流入',
                            type: 'line',
                            symbol: 'none',
                            smooth: 0.5,
                            itemStyle:{
                                normal:{
                                    lineStyle: {
                                        width: 3.5
                                    },
                                    color: '#E87C25'
                                }
                            },
                            data: data_1.reverse()
                        }
                    ]
                }
            })
        }).catch(function (error) {
            alert('未连接到服务器！');
        })
    }

    componentDidMount() {
        this.getData()
        this.interval = setInterval(()=>{
            if(this.props.location.state){
                this.setState({
                    default: "94%"
                })
            }else{
                this.setState({
                    default: "83%"
                })
            }
        },100)
    }

    componentWillUnmount() {
        clearInterval(this.interval)
    }

    render() {
        document.title = "资金统计分析";
        return (
            <div style={{width:this.state.default, height:"100vh", float:"right"}}>
                {/*<p style={{fontSize:"1vw", fontWeight:"bold", margin:"5vh 0 0 13%", float:"left"}}>主力资金流入占比</p>*/}
                {/*<p style={{fontSize:"1vw", fontWeight:"bold", margin:"5vh 0 0 11.5%", float:"left"}}>主力资金流出占比</p>*/}
                {/*<p style={{fontSize:"1vw", fontWeight:"bold", margin:"5vh 0 0 10%", float:"left"}}>所在行业资金流入占比</p>*/}
                {/*<p style={{fontSize:"1vw", fontWeight:"bold", margin:"5vh 0 0 10%", float:"left"}}>所在行业资金流出占比</p>*/}
                {/*<ReactEcharts option={this.state.option_1} notMerge={true}*/}
                {/*              style={{width:"20%", height:"20vh", marginLeft:"8%", float:"left"}}/>*/}
                {/*<ReactEcharts option={this.state.option_2} notMerge={true}*/}
                {/*              style={{width:"20%", height:"20vh", marginLeft:"1%", float:"left"}}/>*/}
                {/*<ReactEcharts option={this.state.option_3} notMerge={true}*/}
                {/*              style={{width:"20%", height:"20vh", marginLeft:"1%", float:"left"}}/>*/}
                {/*<ReactEcharts option={this.state.option_4} notMerge={true}*/}
                {/*              style={{width:"20%", height:"20vh", marginLeft:"1%", float:"left"}}/>*/}
                <div style={{width:"70%", height:"80vh", float:"left", marginTop:'10vh'}}>
                    <ReactEcharts option={this.state.option_2} notMerge={true}
                                  style={{width:"100%", height:"80vh"}}/>
                </div>
                <div style={{width:"20%", height:"92vh", float:"left", marginTop:"5vh"}}>
                    <p style={{fontWeight:"bold", fontSize:"1.2vw"}}>同业资金流入排名前十</p>
                    <Table columns={columns} dataSource={this.state.top10} pagination={pagination} size="small"/>
                    <p style={{fontWeight:"bold", fontSize:"1.2vw"}}>同业资金流入排名后十</p>
                    <Table columns={columns} dataSource={this.state.bottom10} pagination={pagination} size="small" />
                </div>
            </div>
        );
    }
}